package model

import (
	"gorm.io/gorm"
	"time"
	"user_service/global"
)

type Package struct {
	Id          string         `gorm:"column:id;type:varchar(50);not null;" json:"id"`
	Weight      float32        `gorm:"column:weight;type:float(10, 2);not null;" json:"weight"`
	Destination string         `gorm:"column:destination;type:varchar(20);not null;" json:"destination"`
	Urgent      uint           `gorm:"column:urgent;type:bigint;comment:是否加急:1.加急true  2.不加急false ;not null;" json:"urgent"` // 是否加急:1.加急true  2.不加急false
	CreatedAt   time.Time      `gorm:"column:created_at;type:datetime(3);not null;default:CURRENT_TIMESTAMP(3);" json:"created_at"`
	UpdatedAt   time.Time      `gorm:"column:updated_at;type:datetime(3);default:CURRENT_TIMESTAMP(3);" json:"updated_at"`
	DeletedAt   gorm.DeletedAt `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
}

func (p *Package) TableName() string {
	return "package"
}

func (p *Package) Create() error {
	return global.DB.Create(&p).Error
}

func (p *Package) List() (list []*Package, err error) {
	err = global.DB.Find(&list).Where("like destination").Order("weight desc").Error

	if err != nil {
		return nil, err
	}
	return
}

func (p *Package) Info(destination string) error {
	return global.DB.Where(" destination =? ", destination).Find(&p).Error
}

func (p *Package) Infos(urgent uint) error {
	return global.DB.Where("urgent =?", urgent).Find(&p).Error
}
